package app.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import app.Main;

/**
 * Application settings dialog
 * 
 * @author Lukas Vavra
 *
 */
public class SetupDialog extends JDialog {
	
	private JTextField serverUrl;
	
	public SetupDialog(Frame parent) {
		super(parent, "Nastavení klienta", true);
		
		JPanel panel = new JPanel(new GridBagLayout());
		GridBagConstraints cs = new GridBagConstraints();
		
		cs.insets = new Insets(10, 10, 10, 10);
		
		cs.gridx = 0;
		cs.gridy = 0;
		cs.gridwidth = 1;
		panel.add(new JLabel("Adresa serveru: "), cs);
		
		serverUrl = new JTextField(30);
		if(Main.appcnf.getServer() != null) 
			serverUrl.setText(Main.appcnf.getServer());
		cs.gridx = 1;
		cs.gridy = 0;
		cs.gridwidth = 2;
		panel.add(serverUrl, cs);
				
		JPanel butpan = new JPanel(new FlowLayout(FlowLayout.TRAILING, 10, 10));
		JButton butCancel = new JButton("Storno");
		butCancel.addActionListener(new ButCancelListener());
		butpan.add(butCancel);
		
		JButton butOK = new JButton("Uložit");
		butOK.addActionListener(new ButOKListener());
		butpan.add(butOK);
		
		this.add(panel, BorderLayout.CENTER);
		this.add(butpan, BorderLayout.PAGE_END);
		pack();
		setResizable(false);
		setLocationRelativeTo(parent);
		setVisible(true);
	}
	
	private class ButOKListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			Main.appcnf.setServer(serverUrl.getText());
			Main.appcnf.save();
			dispose();
		}
	}
	
	private class ButCancelListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
		
	}

}
